package pl.prv.polanie.client.model;

import java.util.ArrayList;
import java.util.List;


import com.google.gwt.user.client.Command;

public class MultiRequest {
	
	List<Command> finalCommands = new ArrayList<Command>();
	
	private int requestsCount = 0;
	
	public void addFinalCommand(Command cmd) {
		finalCommands.add(cmd);
	}
	
	/**
	 * 
	 * @param requestsCount - liczba requestow, po ktorych powrocie zostana wykonane finalCommandsy.
	 */
	public MultiRequest(int requestsCount) {
		this.requestsCount = requestsCount;
	}
	
	
	public void notifyAboutReturningResponse() {
		requestsCount--;
		
		if (requestsCount == 0) {
//			Planner.log("MultiRequest.notifyAboutReturningResponse: " + "FINAL COMMAND");
			runFinalCommands();
		} else {
//			Planner.log("MultiRequest.notifyAboutReturningResponse: " + "ZA WCZESNIE FINAL COMMAND, TO GO " + requestsCount);
		}
	}
	
	
	private void runFinalCommands() {
		for (int i = 0; i < finalCommands.size(); i++) {
			
			Command cmd = finalCommands.get(i);
			cmd.execute();
		}
	}
	
	
	
	
	

}
